package com.drakeet.multitype.sample.comment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.drakeet.multitype.MultiTypeAdapter
import com.drakeet.multitype.sample.MenuBaseActivity
import com.drakeet.multitype.sample.R
import com.drakeet.multitype.sample.comment.adapter.CommentAdapter
import com.drakeet.multitype.sample.comment.adapter.ReplyAdapter
import kotlinx.android.synthetic.main.activity_comment.*
import com.drakeet.multitype.sample.comment.adapter.LoadMoreAdapter
import com.drakeet.multitype.sample.comment.model.Comment
import com.drakeet.multitype.sample.comment.model.LoadMore
import com.drakeet.multitype.sample.comment.model.Reply

/**
 * Describe:
 * Author: lifei
 * Date: 2020/11/6
 * Email: lifei022454@gtjas.com
 */

class CommentActivity : MenuBaseActivity() {

    internal var multiTypeAdapter: MultiTypeAdapter? = null
    lateinit var items:MutableList<Any>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_comment)

        var commentAdapter = CommentAdapter()
        var replyAdapter = ReplyAdapter()
        var loadMoreAdapter = LoadMoreAdapter()

        items = ArrayList()
        multiTypeAdapter = MultiTypeAdapter()
        multiTypeAdapter!!.register(commentAdapter)
        multiTypeAdapter!!.register(replyAdapter)
        multiTypeAdapter!!.register(loadMoreAdapter)
        multiTypeAdapter!!.items = items

        list.adapter = multiTypeAdapter
        list.layoutManager = LinearLayoutManager(this)

        for (i in 0..20){
            if(i % 2 == 0){
                items.add(Comment())
            }else{
                items.add(Comment())
                for (i in 0..3){
                    items.add(Reply())
                }
                if (i % 3 == 0) {
                    items.add(LoadMore())
                }
            }
        }

        multiTypeAdapter!!.notifyDataSetChanged()

        loadMoreAdapter.onLoadMoreInterface = object : LoadMoreAdapter.onLoadMore {
            override fun onLoadMore(position: Int) {
                for(i in 0..5) {
                    items.add(position, Reply())
                }
                multiTypeAdapter!!.items = items
                multiTypeAdapter!!.notifyItemRangeInserted(position,6)
            }
        }

    }
}